package com.zhoust.threadbatch.service.impl;

import com.zhoust.threadbatch.annotation.LogAttribute;
import com.zhoust.threadbatch.entity.User;
import com.zhoust.threadbatch.mapper.UserMapper;
import com.zhoust.threadbatch.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zhoust
 */
@Service
@Slf4j
public class UserServiceImpl implements IUserService {

    private final UserMapper userMapper;

    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    @Transactional
    public String testRollBackOne() {


        testRollBackTwo();
        testRollBackThree();
        testFore();
        testfive();

        User user = userMapper.selectById(1);
        return user.toString();
    }

    @Override
    @Transactional
    public void testRollBackTwo() {
        User user = new User();
        user.setUsername("张三");
        user.setPassword("123456");
        user.setAge(15);
        testsix();

        int insert = userMapper.insert(user);
    }

    @Override
    @Transactional
    public String testRollBackThree() {
        User user = new User();
        user.setUsername("李氏");
        user.setPassword("123456");
        user.setAge(15);

        int insert = userMapper.insert(user);
        return "testRollBackThree" + insert;
    }

    @Transactional
    public String testFore(){
        User user = new User();
        user.setUsername("王五");
        user.setPassword("123456");
        user.setAge(15);

        int insert = userMapper.insert(user);
        return "testRollBackThree" + insert;
    }

    public String testfive(){
        User user = new User();
        user.setUsername("liusad");
        user.setPassword("123456");
        user.setAge(15);
/*
        int i = 5/0;
*/

        int insert = userMapper.insert(user);
        return "testRollBackThree" + insert;
    }
    private String testsix(){
        User user = new User();
        user.setUsername("7777");
        user.setPassword("123456");
        user.setAge(15);
//        int i = 5/0;

        int insert = userMapper.insert(user);
        return "testRollBackThree" + insert;
    }



    @Override
    public void saveUser(){

        int sum = 1;
        for (int i =0;i<50000;i++){
            User user = new User();
            user.setId(10000+i);
            user.setUsername("嘉文"+i);
            user.setPassword("10000"+i);
            user.setAge(5555);
            userMapper.insert(user);
            sum+=i;
            if(i == 49999){
                log.info("插入{}条",sum);
            }
        }

    }
    @Override
    @LogAttribute
    public List<User> queryUser(List<Integer> ids){


        for (int i =0;i<50000;i++){

            ids.add(10000+i);

        }
        List<User> users = userMapper.selectBatchIds(ids);
        return users;

    }
}
